<?php


namespace App\Http\Model\Job;


use App\Enum\ClassListHistoryEnum;
use Illuminate\Support\Facades\DB;

class PfsHeadAddBatchClassModel
{

    /**
     * 班级信息同步-同步结果入库
     *
     * @param $data
     * @param $result
     *
     * @return bool
     */
    public static function save($data, $result, $errMsg = '')
    {
        $list = [];
        foreach ($data['list'] as $val) {
            if (isset($result[$val->class_id]->success)) {
                $vResult = $result[$val->class_id]->success === true ? ClassListHistoryEnum::RESULT_SUCCESS : ClassListHistoryEnum::RESULT_FAILURE;
            } else {
                $vResult = ClassListHistoryEnum::RESULT_UNKNOW;
            }
            $ins = [
                'lessons'              => $val->lessons,
                'push_time'            => date('Y-m-d H:i:s'),
                'pfs_dw_id'            => $data['pfs_dw_id'],
                'org_class_id'         => $val->org_class_id,
                'class_id'             => $val->class_id,
                'class_name'           => $val->class_name,
                'stages_id'            => $val->stages_id,
                'stages_num'           => $val->stages_num,
                'begin_date'           => $val->begin_date,
                'end_date'             => $val->end_date,
                'class_type'           => $val->class_type,
                'ledger_subject_name'  => $val->ledger_subject_name,
                'ledger_subject_id'    => $val->ledger_subject_id,
                'city_name'            => $val->city_name,
                'area_code'            => $val->area_code,
                'course_max_num'       => $val->course_max_num,
                'course_contacts'      => $val->course_contacts,
                'contacts_information' => $val->contacts_information,
                'course_addr'          => $val->course_addr,
                'teacher_name'         => $val->teacher_name,
                'course_desc'          => $val->course_desc,
                'course_type_name'     => $val->course_type_name,
                'business_type'        => $val->business_type,
                'create_date'          => $val->create_date,
                'modify_date'          => $val->modify_date,
            ];
            $arr = array_merge($ins, [
                'uniqid'      => $val->uniqid,
                'result'      => $vResult,
                'result_desc' => empty($result[$val->class_id]->desc) ? $errMsg : $result[$val->class_id]->desc,
            ]);
            /*查询记录是否已经存在*/
            $exist = DB::select('SELECT `id` FROM `class_list_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `modify_date`=:modify_date AND `is_del`=0 LIMIT 1', [
                'pfs_dw_id'   => $data['pfs_dw_id'],
                'class_id'    => $val->class_id,
                'modify_date' => $val->modify_date,
            ]);
            /*如果记录不存在,则新增*/
            if (empty($exist)) {
                $list[] = $arr;
            } else {
                /*更新记录*/
                DB::table('class_list_history')->where('id', $exist[0]->id)->update($arr);
            }
            /*更新班级表*/
            $id = DB::select('SELECT `id` FROM `class_list` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `is_del`=0', [
                'pfs_dw_id' => $data['pfs_dw_id'],
                'class_id'  => $val->class_id,
            ]);
            if (empty($id)) {
                DB::table('class_list')->insert($ins);
            } else {
                $id = current($id)->id;
                DB::table('class_list')->where('id', $id)->update($ins);
            }
        }
        if (empty($list)) {
            return 0;
        }
        /*写入同步记录表*/
        return DB::table('class_list_history')->insert($list);
    }

}





/**
 * PfsHeadAddBatchClassModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/5 20:19:00   操作:创建
 **/
